package com.zysf.library_boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysf.library_boot.entity.BookInfo;
import com.zysf.library_boot.entity.ReaderInfo;
import com.zysf.library_boot.entity.TypeInfo;
import com.zysf.library_boot.service.TypeInfoService;
import com.zysf.library_boot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/typeInfo")
public class TypeInfoController {
    @Autowired
    TypeInfoService typeInfoService;

    @RequestMapping("/getTypeInfo")
    public Result getTypeInfo(Integer currentPage, Integer pageSize, String name) {
        Page<TypeInfo> page = new Page<>(currentPage, pageSize);
        QueryWrapper<TypeInfo> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name); // 模糊查询类型名称
        }
        Page<TypeInfo> typeInfoPage = typeInfoService.page(page, queryWrapper);
        return Result.ok(typeInfoPage);
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        boolean c = typeInfoService.removeById(id);
        return Result.ok(c);
    }

    @RequestMapping("/batchDeleteType")
    public Result batchDeleteType(@RequestBody List<Integer> ids){
        boolean d = typeInfoService.removeBatchByIds(ids);
        return Result.ok(d);
    }

    @PostMapping("/addType")
    public Result addType(@RequestBody TypeInfo typeInfo) {
        boolean saved = typeInfoService.save(typeInfo);
        if (saved) {
            return Result.ok("添加成功");
        } else {
            return Result.fail("添加失败");
        }
    }

    @PutMapping("/updateType")
    public Result updateType(@RequestBody TypeInfo typeInfo) {
        boolean updated = typeInfoService.updateById(typeInfo);
        if (updated) {
            return Result.ok("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }

    @RequestMapping("/getAllTypeNames")
    public Result getAllTypeNames(){
        List<TypeInfo> typeInfoList = typeInfoService.list();
        return Result.ok(typeInfoList);
    }
}
